package cn.sinjet.carassist;

import java.util.List;

import com.mgroup.wazehelper.R;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import cn.sinjet.adapter.ShortcutAdapter;
import cn.sinjet.entity.AppInfo;
import cn.sinjet.model.carassist.AppModel;
import cn.sinjet.utils.AppInfoUtil;
/**
 * poi搜索界面
 * @author Kevin
 */
public class ChooseShortcutActivity extends MyActivity implements OnClickListener{
	List<AppInfo> mAppInfoList = null;
	GridView mAppGridView;
	ShortcutAdapter mShortcutAdapter = null;
//	ViewGroup mViewRoot;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		LayoutInflater inflater = LayoutInflater.from(this);
//		mViewRoot = (ViewGroup)inflater.inflate(R.layout.activity_choose_shortcut,null);
		super.setMyContentView(R.layout.activity_choose_shortcut);
//		setContentView(mViewRoot);
		init(savedInstanceState);
	}

	private void init(Bundle savedInstanceState) {
		mViewRoot.findViewById(R.id.btn_back).setOnClickListener(this);
		initAppInfoList();
	}
	
	private void onAppItemClick(AppInfo appInfo){
		  if(appInfo == null) {
			  ChooseShortcutActivity.this.setResult(-1);
			  ChooseShortcutActivity.this.finish();
			  return;
		  }
		  int shortcutId = ChooseShortcutActivity.this.getIntent().getIntExtra("ShortcutId", -1);
		  Log.d("navi", "shortcutId ="+shortcutId);
		  AppModel.getInstance().selectNewApp(shortcutId,appInfo);
		  ChooseShortcutActivity.this.setResult(0,ChooseShortcutActivity.this.getIntent().putExtra("ShortcutId", shortcutId));
		  ChooseShortcutActivity.this.finish();
	}
	
	private void initAppInfoList() {
		// TODO Auto-generated method stub
		mAppGridView = (GridView)mViewRoot.findViewById(R.id.search_results);
	    mAppInfoList = AppInfoUtil.getAppList(this);
	    mShortcutAdapter = new ShortcutAdapter(mAppInfoList, this);
	    mAppGridView.setAdapter(mShortcutAdapter);
	    mAppGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int pos,long id) {
				// TODO Auto-generated method stub
				if(mAppInfoList == null)
					return;
				if(pos<mAppInfoList.size()){
           	    	  onAppItemClick(mAppInfoList.get(pos));
           	    }else{ 
           		 ChooseShortcutActivity.this.setResult(-1);
           	     ChooseShortcutActivity.this.finish();
			}
	      }
		});
	}
	
	
   @Override
protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
}
	
   	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}   
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			    onBack();
		}
		return super.onKeyUp(keyCode, event);
	}

	private void onBack(){
		ChooseShortcutActivity.this.setResult(-1);
		ChooseShortcutActivity.this.finish(); 	   
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_back:
		       onBack();
		 break;
		}
	}

}
